package com.putao.album.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.util.StringHelper;
import com.putao.album.util.ViewHelper;

/**
 * Created by chen on 15/5/13.
 */
public class SelectDialogBuilder extends DialogBuilder {

    private DialogButtonOnClickListener mDialogButtonOnClickListener;

    protected SelectDialogBuilder(Context context) {
        super(context);
    }

    public SelectDialogBuilder setDialogButtonOnClickListener(DialogButtonOnClickListener listener) {
        mDialogButtonOnClickListener = listener;
        return this;
    }

    public interface DialogButtonOnClickListener {
        void positiveOnClick(Dialog dialog);

        void negativeOnClick(Dialog dialog);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.dialog_photo_select_layout;
    }

    @Override
    protected void doInitSubView(View view) {
        LinearLayout positive_ll = ViewHelper.queryViewById(view, R.id.camera_ll);
        TextView positive_tv = ViewHelper.queryViewById(view, R.id.camera_tv);

        LinearLayout negative_ll = ViewHelper.queryViewById(view, R.id.album_ll);
        TextView negative_tv = ViewHelper.queryViewById(view, R.id.album_tv);
        if (!StringHelper.isEmpty(getPositiveButtonText())) {
            positive_tv.setText(positiveButtonText);
            positive_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDialogButtonOnClickListener != null) {
                        mDialogButtonOnClickListener.positiveOnClick(getDialog());
                    }
                }
            });

        } else {
            positive_ll.setVisibility(View.GONE);
        }

        if (!StringHelper.isEmpty(getNegativeButtonText())) {
            negative_tv.setText(negativeButtonText);
            negative_ll.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (mDialogButtonOnClickListener != null) {
                        mDialogButtonOnClickListener.negativeOnClick(getDialog());
                    }
                }
            });
        } else {
            negative_ll.setVisibility(View.GONE);
        }

        //设置底部对齐,边距为0
        Window win = getDialog().getWindow();
        win.setGravity(Gravity.BOTTOM);
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
    }

    @Override
    protected void doSetupData() {

    }
}
